package com.demo.service.impl;

import com.demo.entity.Carousel;
import com.demo.entity.Category;
import com.demo.entity.Product;
import com.demo.mapper.CarouselMapper;
import com.demo.mapper.ProductMapper;
import com.demo.service.CarouselService;
import com.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper mapper;

    @Override
    public List<Product> findAllProduct() {
        return mapper.findAllProduct();
    }

    @Override
    public List<Product> findProductByCategoryId(Integer categoryId) {
        return mapper.findProductByCategoryId(categoryId);
    }

    @Override
    public List<Category> findAllCategory() {
        return mapper.findAllCategory();
    }

    @Override
    public int insertProduct(Product product) {
        mapper.insertProduct(product);
        return mapper.findLastInsertId();
    }

    @Override
    public int updateProductPicture(int productId, List<String> productPicture) {
        mapper.deleteProductPicture(productId);
        productPicture.remove(productPicture.size() - 1);
        productPicture.forEach((item) -> {
            mapper.insertProductPicture(productId, item);
        });
        return 0;
    }

    @Override
    public Product findProductById(Integer productId) {
        Product product = mapper.findProductById(productId);
        if (mapper.findAllPictureByProductId(productId).size() > 0)
            product.setProductList(mapper.findAllPictureByProductId(productId));
        return product;
    }

    @Override
    public int updateProduct(Product product) {
        return mapper.updateProduct(product);
    }

    @Override
    public int deleteProductById(Integer productId) {
        return mapper.deleteProductById(productId);
    }


}
